package com.yuanrui.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.yuanrui.subject.common.enums.CategoryTypeEnum;
import com.yuanrui.subject.common.enums.IsDeletedFlagEnum;
import com.yuanrui.subject.domain.convert.SubjectLabelConvert;
import com.yuanrui.subject.domain.entity.SubjectLabelBo;
import com.yuanrui.subject.domain.service.SubjectLabelDomainService;
import com.yuanrui.subject.infra.basic.entity.SubjectCategory;
import com.yuanrui.subject.infra.basic.entity.SubjectLabel;
import com.yuanrui.subject.infra.basic.entity.SubjectMapping;
import com.yuanrui.subject.infra.basic.service.SubjectCategoryService;
import com.yuanrui.subject.infra.basic.service.SubjectLabelService;
import com.yuanrui.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public Boolean addLabel(SubjectLabelBo subjectLabelBo) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelController.add.bo:{}", JSON.toJSONString(subjectLabelBo));
        }
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToLabel(subjectLabelBo);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int insert = subjectLabelService.insert(subjectLabel);
        return insert > 0;

    }

    /**
     * 修改标签
     * @param subjectLabelBo 标签信息
     * @return 是否成功
     */
    @Override
    public Boolean updateLabel(SubjectLabelBo subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToLabel(subjectLabelBo);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 删除标签
     * @param subjectLabelBo 标签信息
     * @return  是否成功
     */
    @Override
    public Boolean deleteLabel(SubjectLabelBo subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToLabel(subjectLabelBo);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 根据分类id查询标签
     * @param subjectLabelBo 分类id
     * @return 标签信息
     */
    @Override
    public List<SubjectLabelBo> queryLabelByCategoryId(SubjectLabelBo subjectLabelBo) {
        //如果分类id为一级分类id，则返回所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBo.getCategoryId());
        if (subjectCategory.getCategoryType()== CategoryTypeEnum.PRIMARY.getCode()){
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            List<SubjectLabel> subjectLabels = subjectLabelService.queryByCondition(subjectLabel);
            return SubjectLabelConvert.INSTANCE.convertLabelToBo(subjectLabels);
        }
        Long categoryId = subjectLabelBo.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> subjectMappings= subjectMappingService.queryLabelByCategoryId(subjectMapping);
        if (subjectMappings.isEmpty()) {
            return Collections.emptyList();
        }
        //获取标签id
        List<Long> labelList = subjectMappings.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> subjectLabels = subjectLabelService.batchQueryById(labelList);
        //转换为bo
        List<SubjectLabelBo> subjectLabelBos = new ArrayList<>();
        for (SubjectLabel subjectLabel : subjectLabels) {
            SubjectLabelBo labelBo = new SubjectLabelBo();
            labelBo.setId(subjectLabel.getId());
            labelBo.setLabelName(subjectLabel.getLabelName());
            labelBo.setCategoryId(subjectLabel.getCategoryId());
            labelBo.setIsDeleted(subjectLabel.getIsDeleted());
            labelBo.setSortNum(subjectLabel.getSortNum());
            subjectLabelBos.add(labelBo);
        }

        return subjectLabelBos;
    }
}
